<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>京东(JD.COM)-综合网购首选-正品低价、品质保障、配送及时、轻松购物！</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="css/normalize.css" />
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/cart.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/base.js"></script>
    <script src="js/cart.js"></script>
    <script src="js/axios.min.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<style>
    .mask{
        background-color: black;
    }
</style>
<body>
    <div id="app">
        <header>
            <a href="#">
                <img src="images/header.jpg" height="80" width="100%" alt="">
            </a>
        </header>
        <div>
            <iframe MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=no src="heade.html" height="30px" width="100%"></iframe>
               </div>
        <div class="m">
            <div class="w middle">
                <div class="logo">
                    <h1>
                        <!-- 提高权重 便利于搜索引擎优化 -->
                        <a href="index.html" title="京东">京东</a>
                    </h1>
                </div>
                <div class="form">
                    <input id="text" type="text" placeholder="扫描仪">
                    <button><i></i></button>
                </div>
                <div class="shopCar">
                    <i></i><a href="cart.html" class="f10">我的购物车</a><span>8</span>
                </div>
                <div class="hotwords">
                    <a href="#" class="f10">199减100</a>
                    <a href="#">鼠标试用</a>
                    <a href="#">农资7折</a>
                    <a href="#">低至29元</a>
                    <a href="#">抽奖赢空调</a>
                    <a href="#">记忆棉</a>
                    <a href="#">坐垫</a>
                    <a href="#">1分钱买油</a>
                    <a href="#">智能手表</a>
                </div>
                <div class="navitems">

                </div>
                <div class="super">
                    <a href="#"><img src="images/super.png" alt=""></a>
                </div>
            </div>
        </div>
        <div class="py-container">
            <div class="w">
                <div class="allgoods">
                    <h4>全部商品
                        <span></span>
                    </h4>
                    <div class="cart-main">
                        <div class="cart-th clearfix">
                            <div><input type="checkbox">全选</div>
                            <div class="">商品</div>
                            <div class="">单价（元）</div>
                            <div class="">数量</div>
                            <div class="">小计（元）</div>
                            <div class="">操作</div>
                        </div>

                        <div class="cart-item-list">
                            <div class="cart-body">
                                <div class="cart-list">
                                        <ul class="goods-list yui3-g" v-for="(commodity, index) in commodityList"
                                        :key="commodity.commodityInfo.itemNumber">
                                        <li class="yui3-u-3-8 pr">
                                                    <input type="checkbox" v-if='commodity.commodityInfo.stock == 0' disabled :value="commodity.commodityInfo.itemNumber" @change="selectChange(commodity.commodityInfo.itemNumber,index,commodity.commodityInfo.price,commodity.number)" class="good-checkbox">
                                                    <input type="checkbox" v-if='commodity.commodityInfo.stock != 0' :value="commodity.commodityInfo.itemNumber" @change="selectChange(commodity.commodityInfo.itemNumber,index,commodity.commodityInfo.price,commodity.number)" class="good-checkbox">
                                                    <div class="good-item">
                                                        <a :href="gotoDetail(commodity.commodityInfo.itemNumber)"
                                                            target="_blank">
                                                            <div class="item-img">
                                                                <img :src="commodity.commodityInfo.exhibition">
                                                            </div>
                                                        </a>
                                                        <div class="item-msg">{{commodity.commodityInfo.exName}}</div>
                                                    </div>
                                                </li>
                                                <li class="yui3-u-1-8">
                                                    <span>颜色: 银色</span>
                                                    <br>
                                                    <br>
                                                    <span>规格: {{commodity.commodityInfo.specifications}}</span>
                                                </li>
                                                <li class="yui3-u-1-8">
                                                    <span class="price">{{commodity.commodityInfo.price}}</span>
                                                </li>
                                                <li class="yui3-u-1-8">
                                                    <div class="clearfix">
                                                        <el-button v-if="commodity.commodityInfo.stock == 0" disabled
                                                            size='small' @click='reduce(commodity.number,index)'
                                                            style="height: 46px;">-</el-button>
                                                        <el-button v-if="commodity.commodityInfo.stock != 0" size='small'
                                                            @click='reduce(commodity.number,index)' style="height: 46px;">-
                                                        </el-button>
                                                        <input type="text" size="1" min="1" max="199"
                                                            style="border: 1px solid white;height: 44px;text-align: center;"
                                                            v-model='commodity.number'>
                                                        <el-button v-if="commodity.commodityInfo.stock == 0" disabled
                                                            size='small' @click='add(commodity.number,index)'
                                                            style="height: 46px;">+</el-button>
                                                        <el-button v-if="commodity.commodityInfo.stock != 0" size='small'
                                                            @click='add(commodity.number,index)' style="height: 46px;">+
                                                        </el-button>
                                                    </div>
                                                    <div class="youhuo" style="color: red;"
                                                        v-if="commodity.commodityInfo.stock <= 100 && commodity.commodityInfo.stock > 0">
                                                        仅剩 {{commodity.commodityInfo.stock}}件</div>
                                                    <div class="youhuo" style="color: rgb(77, 75, 75);"
                                                        v-if="commodity.commodityInfo.stock == 0">无货</div>
                                                </li>
                                                <li class="yui3-u-1-8">
                                                    <span class="sum">{{commodity.commodityInfo.price *
                                                        commodity.number}}</span>
                                                </li>
                                                <li class="yui3-u-1-8">
                                                    <div class="del1">
                                                        <a @click='deleCartCommodity(commodity.commodityInfo.itemNumber,index)' href="javascript:;">删除</a>
                                                    </div>
                                                </li>
                                            
                                            
                                        </ul>
                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="cart-tool clearfix">
                        <div class="money-box">
                            <div class="sumprice">
                                <div class="sumprice-top">
                                    <span>
                                        已选择
                                        <strong>{{countNumber}}</strong> 件商品</span>
                                    <span>
                                        <em>总价：</em>
                                        <span style="color: #e54046;">￥{{countPrice}}</span>
                                </div>
                              
                            </div>
                            <div class="sumbtn">
                                <a class="sum-btn" @click='settlement'>结算</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <footer>
            <div class="service">
                <!-- 服务模块 -->
                <div class="w">
                    <ul>
                        <li>
                            <h5></h5>
                            <p>品类齐全，轻松购物</p>
                        </li>
                        <li>
                            <h5></h5>
                            <p>品类齐全，轻松购物</p>
                        </li>
                        <li>
                            <h5></h5>
                            <p>品类齐全，轻松购物</p>
                        </li>
                        <li>
                            <h5></h5>
                            <p>品类齐全，轻松购物</p>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- 帮助模块 -->
            <div class="w help">
                <div class="fl">
                    <dl>
                        <dt>购物指南</dt>
                        <dd><a href="#">购物流程</a></dd>
                        <dd><a href="#">会员介绍</a></dd>
                        <dd><a href="#">生活旅行</a></dd>
                        <dd><a href="#">常见问题</a></dd>
                        <dd><a href="#">大家电</a></dd>
                        <dd><a href="#">联系客服</a></dd>
                    </dl>
                    <dl>
                        <dt>配送方式</dt>
                        <dd><a href="#">上门自提</a></dd>
                        <dd><a href="#">211限时达</a></dd>
                        <dd><a href="#">配送服务查询</a></dd>
                        <dd><a href="#">配送费收取标准</a></dd>
                        <dd><a href="#">海外配送</a></dd>
                    </dl>
                    <dl>
                        <dt>支付方式</dt>
                        <dd><a href="#">货到付款</a></dd>
                        <dd><a href="#">在线支付</a></dd>
                        <dd><a href="#">分期付款</a></dd>
                        <dd><a href="#">邮局汇款</a></dd>
                        <dd><a href="#">公司转账</a></dd>
                    </dl>
                    <dl>
                        <dt>售后服务</dt>
                        <dd><a href="#">售后政策</a></dd>
                        <dd><a href="#">价格保护</a></dd>
                        <dd><a href="#">退款说明</a></dd>
                        <dd><a href="#">返修/退换货</a></dd>
                        <dd><a href="#">取消订单</a></dd>
                    </dl>
                    <dl>
                        <dt>特色服务</dt>
                        <dd><a href="#">夺宝岛</a></dd>
                        <dd><a href="#">DIY装机</a></dd>
                        <dd><a href="#">延保服务</a></dd>
                        <dd><a href="#">京东E卡</a></dd>
                        <dd><a href="#">京东通信</a></dd>
                        <dd><a href="#">京鱼座智能</a></dd>
                    </dl>
                </div>
                <div class="fr coverage">
                    <h5>京东自营覆盖区县</h5>
                    <p>京东已向全国2661个区县提供自营配送服务，支持货到付款、POS机刷卡和售后上门服务。</p>
                    <a href="#">查看详情 > </a>
                </div>
            </div>
            <div class="w copyright">
                <p>
                    <a href="#">关于我们</a>
                    <span> | </span>
                    <a href="#">联系我们</a>
                    <span> | </span>
                    <a href="#">联系客服</a>
                    <span> | </span>
                    <a href="#">合作招商</a>
                    <span> | </span>
                    <a href="#">商家帮助</a>
                    <span> | </span>
                    <a href="#">营销中心</a>
                    <span> | </span>
                    <a href="#">手机京东</a>
                    <span> | </span>
                    <a href="#">友情链接</a>
                    <span> | </span>
                    <a href="#">销售联盟</a>
                    <span> | </span>
                    <a href="#">京东社区</a>
                    <span> | </span>
                    <a href="#">风险监测</a>
                    <span> | </span>
                    <a href="#">隐私政策</a>
                    <span> | </span>
                    <a href="#">京东公益</a>
                    <span> | </span>
                    <a href="#">English Site</a>
                    <span> | </span>
                    <a href="#">Media & IR</a>
                </p>
                <div>
                    <p>京公网安备
                        11000002000088号<span>|</span>京ICP证070359号<span>|</span>互联网药品信息服务资格证编号(京)-经营性-2014-0008<span>|</span>新出发京零
                        字第大120007号</p>
                    <p>互联网出版许可证编号新出网证(京)字150号<span>|</span>出版物经营许可证<span>|</span>网络文化经营许可证京网文[2014]2148-348号<span>|</span>违法和不良信息举报电话：4006561155
                    </p>
                    <p>Copyright © 2004 - 2019 京东JD.com
                        版权所有<span>|</span>消费者维权热线：4006067733经营证照|(京)网械平台备字(2018)第00003号<span>|</span>营业执照</p>
                    <p>Global Site<span>|</span>Сайт России<span>|</span>Situs Indonesia<span>|</span>Sitio de
                        España<span>|</span>เว็บไซต์ประเทศไทย</p>
                    <p>京东旗下网站：京东钱包<span>|</span>京东云</p>
                </div>
                <p class="foot-icon">
                    <a href="#"></a>
                    <a href="#"></a>
                    <a href="#"></a>
                    <a href="#"></a>
                    <a href="#"></a>
                    <a href="#"></a>
                </p>
            </div>
        </footer>
    </div>
</body>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                commodityList: {},//存放商品对象
                token: '',//用户id
                countNumber: 0,
                countPrice: 0,
            }
        },
        created() {
            this.showInfo()
            this.getCrtComByUerId();
        },
        methods: {
            showInfo() {
                let token = this.getCookie("token");
                if (token == "" || token == null) {
                    // window.location.href = 'login.html';
                }else{
                    this.token = token;
                }
            },
            //根据用户id查询购物车信息
            getCrtComByUerId() {
                let param = new URLSearchParams()
                param.append('token', this.token)
                axios.post(`http://localhost/admin/user/cart/getCrtComByUerId`,param)
                    .then(response => {
                        let resData = response.data;
                        this.commodityList = resData.data;
                    })
            },
            //跳转至详情页
            gotoDetail(itemNumber) {
                return 'detail.html?itemNumber=' + itemNumber;
            },
            //添加购物车中的商品数量
            add(number, index) {
                if (number < this.commodityList[index].commodityInfo.stock && number < 199) {
                    this.commodityList[index].number++;
                    let itemNumber = this.commodityList[index].commodityInfo.itemNumber
                    let number = this.commodityList[index].number;

                    this.updateCarNum(itemNumber, number);
                }
            },
            //减少购物车中的商品数量
            reduce(number, index) {
                if (number > 1) {
                    this.commodityList[index].number--;
                    let itemNumber = this.commodityList[index].commodityInfo.itemNumber
                    let number = this.commodityList[index].number;

                    this.updateCarNum(itemNumber, number);
                }
            },
            //更新购物车中的商品数量
            updateCarNum(itemNumber, number) {
                let param = new URLSearchParams()
                param.append('itemNumber', itemNumber)
                param.append('number', number)
                param.append('token', this.token)
                axios.post(`http://localhost/admin/user/cart/updateCarNum`,param)
            },
            //更新总价与总数量
            selectChange(itemNumber,index,price,number){
                var check = $(".good-checkbox").eq(index).prop("checked");
                if(check){
                    this.countPrice += price * number;
                    this.countNumber += number;
                }else{
                    this.countPrice -= price * number;
                    this.countNumber -= number;
                }
            },
            //删除购物车的商品
            deleCartCommodity(itemNumber,index){
                let param = new URLSearchParams()
                param.append('token', this.token)
                param.append('itemNumber', itemNumber)
                axios.delete(`http://localhost/admin/user/cart/delCartComm`,
                    {data:param}
                ).then(response => {
                    $($(".cart-list").find("ul")[index].remove())
                })         
            },
            //结算，创建订单
            settlement(){
                //获取被勾选的商品编号
                var check = $(".good-checkbox:checked");
                let itemNumbers = [];
                for (let i = 0; i < check.length; i++) {
                    itemNumbers.push(check[i].value);
                }
                if(itemNumbers.length == 0){
                    return;
                }
                let param = new URLSearchParams()
                param.append('token', this.token)
                param.append('itemNumbers', itemNumbers)
                axios.post(`http://localhost/admin/order/saveOrder`, param).then(response => {
                    let orderId = response.data.data;
                    window.location.href = 'pay.html?orderId='+orderId;
                })
            },
            GetURLParameter(sParam) {
                let sPageURL = window.location.search.substring(1);
                let sURLVariables = sPageURL.split("&");
                for (let i = 0; i < sURLVariables.length; i++) {
                    let sParameterName = sURLVariables[i].split('=');
                    if (sParameterName[i] == sParam) {
                        return sParameterName[i + 1];
                    }
                }
            },
            getCookie(name) {
                var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
                if (arr = document.cookie.match(reg)) {
                    return unescape(arr[2]);

                } else {
                    return null;
                }
            }
        }
    })
</script>

</html>